<template>
	<div>
		<picker id="picker" mode="multiSelector" :range="range" :value="value" @columnchange="columnchange"
			@cancel="pickerCancel" @change="getcity">
			<slot></slot>
		</picker>
	</div>
</template>

<script>
	import {
		area
	} from './area.js'
	export default {
		data() {
			return {
				selected: '',
				range: [
					[''],
					[''],
					['']
				],
				provinceCodes: [],
				cityCodes: [],
				citycode: '',
				value: [0, 0, 0],
				cityarr: [],
				checkboxs: [],
				address: ''
			};
		},
		methods: {
			pickerCancel() {
				console.log('pickerCancel')
			},
			// 获取县区id
			getcode() {
				var that = this;
				if (that.cityarr[that.cityarr.length - 1]) {
					var obj = area.county_list
					if(that.cityarr.length==1){
						var obj = area.province_list
					}
					if(that.cityarr.length==2){
						var obj = area.city_list
					}
					if(that.cityarr.length==3){
						var obj = area.county_list
					}
					Object.keys(obj).forEach(function(key) {
						if (that.cityarr[that.cityarr.length - 1] == obj[key]) {
							that.citycode = key
						}
					});
				}
				that.$emit('getdata', {
					data: that.cityarr,
					code: that.citycode,
					address: this.address,
				})
			},
			getcity(e) {
				console.log(e.detail.value)
				var cityselect = e.detail.value
				this.cityarr = []
				var list = [0, 1, 2]
				list.forEach((type) => {
					if (this.range[type][cityselect[type]]) {
						this.cityarr.push(this.range[type][cityselect[type]])
					}
				});
				this.address = this.range[0][cityselect[0]] + " " + this.range[1][cityselect[1]] + " " +
					this.range[2][cityselect[2]]
				this.getcode()
			},
			columnchange: function(e) {
				this.value[e.detail.column] = e.detail.value
				this.selected = ''
				if (0 == e.detail.column) {
					let provinceCode = this.provinceCodes[e.detail.value - 1]
					this.range[1] = ['']
					this.range[2] = ['']
					let cities = ['']
					this.cityCodes = []
					for (let cityCode in area.city_list) {
						if (Number(cityCode) >= Number(provinceCode) && Number(cityCode) <= Number(provinceCode) +
							9900) {
							cities.push(area.city_list[cityCode])
							this.cityCodes.push(cityCode)
						}
					}
					this.range[1] = cities
					this.value.splice(1, 1, 0)
					this.value.splice(2, 1, 0)
				} else if (1 == e.detail.column) {
					this.value[2] = 0
					let cityCode = this.cityCodes[e.detail.value - 1]
					this.range[2] = ['']
					let counties = ['']
					for (let countyCode in area.county_list) {
						if (Number(countyCode) >= Number(cityCode) && Number(countyCode) <= Number(cityCode) + 99) {
							counties.push(area.county_list[countyCode])
						}
					}
					this.range[2] = counties

					this.value.splice(2, 1, 0)
				}
				this.$forceUpdate()
				if (this.range[2][this.value[2]]) {
					this.selected = this.range[2][this.value[2]]
				} else if (this.range[1][this.value[1]]) {
					this.selected = this.range[1][this.value[1]]
				} else if (this.range[0][this.value[0]]) {
					this.selected = this.range[0][this.value[0]]
				}
			}
		},
		mounted: function() {
			for (let provinceCode in area.province_list) {
				this.range[0].push(area.province_list[provinceCode])
				this.provinceCodes.push(provinceCode)
			}
		}
	};
</script>

<style>
</style>
